package com.gitee.linmt.service.impl;

import com.gitee.linmt.entity.SysRolePermission;
import com.gitee.linmt.repository.SysRolePermissionRepository;
import com.gitee.linmt.service.SysRolePermissionService;
import org.springframework.stereotype.Service;

/**
 * 角色-权限 Service 实现类.
 *
 * @author <a href="mailto:lin-mt@outlook.com">lin-mt</a>
 */
@Service
public class SysRolePermissionServiceImpl implements SysRolePermissionService {
    
    private final SysRolePermissionRepository rolePermissionRepository;
    
    public SysRolePermissionServiceImpl(SysRolePermissionRepository rolePermissionRepository) {
        this.rolePermissionRepository = rolePermissionRepository;
    }
    
    @Override
    public SysRolePermission saveOrUpdate(SysRolePermission rolePermission) {
        SysRolePermission exist = rolePermissionRepository
                .getByRoleIdAndPermissionId(rolePermission.getRoleId(), rolePermission.getPermissionId());
        if (exist != null) {
            rolePermission.setId(exist.getId());
        }
        return rolePermissionRepository.save(rolePermission);
    }
    
    @Override
    public boolean delete(Long deleteId) {
        rolePermissionRepository.deleteById(deleteId);
        return true;
    }
}
